package com.school.campus.task;

import com.school.campus.domain.StudyRoom;
import com.school.campus.mapper.StudyRoomMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Component
public class StudyRoomTask {
    @Autowired
    private StudyRoomMapper studyRoomMapper;

    @Scheduled(cron = "0 * * * * *")
    public void roomStatusTask() {
        // 获取开放时间段
        List<StudyRoom> allStudyRoom = studyRoomMapper.selectStudyRoomList(null);

        List<StudyRoom> studyRoomList = new ArrayList<>();
        for (StudyRoom studyRoom : allStudyRoom) {
            String openTime = studyRoom.getOpenTime();

            String[] s = openTime.split("至");
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm");
            LocalTime begin = LocalTime.parse(s[0], dateTimeFormatter);
            LocalTime end = LocalTime.parse(s[1], dateTimeFormatter);
            if (LocalTime.now().isAfter(begin) && LocalTime.now().isBefore(end)) {
                studyRoom.setStatus(1L);
            }else {
                studyRoom.setStatus(0L);
            }


            studyRoomList.add(studyRoom);
        }
        studyRoomMapper.BatchUpdateStudyRoom(studyRoomList);


    }
}